﻿Public Class F14_The_Tin_Dung

    Dim DS As New List(Of TheTinDung)

    Sub HienThi()
        lsv_dsthe.Items.Clear()
        DS = XL_TheTinDung.LayDS(Me.Tag)
        For Each t In DS
            Dim li As New ListViewItem(t.NgayLap.ToShortDateString())
            li.Tag = t
            li.SubItems.Add(t.TaiKhoan)
            li.SubItems.Add(t.NguoiDung)
            li.SubItems.Add(t.SoTien)
            li.SubItems.Add(t.HanMuc)
            li.SubItems.Add(t.LaiSuat)
            lsv_dsthe.Items.Add(li)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (lsv_dsthe.SelectedItems.Count > 0) Then
            Try
                Dim f As New F15B_tra_Tin_Dung
                f.Tag = lsv_dsthe.SelectedItems(0).Tag
                f.ShowDialog()
            Catch ex As Exception

            End Try
        Else
            MessageBox.Show("Chưa chọn thẻ tín dụng!", "Chú ý!")
        End If
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub F14_The_Tin_Dung_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label_nguoidung.Text = XL_NguoiDung.Lay(Me.Tag).Username
        Dim dst As New List(Of CTTraTD)
        DS = XL_TheTinDung.LayDS(Me.Tag)
        For Each t As TheTinDung In DS
            dst = XL_CTTraTD.LayDS(t.ID)
            Dim cntn As Integer = XL_CTTraTD.TinhTongNo(dst)
        Next
        HienThi()
    End Sub

    Private Sub btngd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngd.Click
        If (lsv_dsthe.SelectedItems.Count > 0) Then
            Try
                Dim f As New F15_GiaoDichTinDung
                f.Tag = lsv_dsthe.SelectedItems(0).Tag
                f.ShowDialog()
            Catch ex As Exception

            End Try
        Else
            MessageBox.Show("Chưa chọn thẻ tín dụng!", "Chú ý!")
        End If
        
    End Sub

    Private Sub btnthemtk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthemtk.Click
        Try
            If txttenthe.Text <> "" Then
                Dim kqkt = XL_TheTinDung.KTTen(txttenthe.Text)
                If (kqkt <> 0) Then
                    MessageBox.Show("Tên thẻ đã được sử dụng!")
                Else
                    Dim t As New TheTinDung
                    t.NgayLap = dtp_ngaylap.Value.Date
                    t.TaiKhoan = txttenthe.Text
                    t.NguoiDung = Me.Tag
                    t.SoTien = Integer.Parse(txt_sotien.Text)
                    t.HanMuc = Integer.Parse(txt_hanmuc.Text)
                    t.LaiSuat = Integer.Parse(txt_laisuat.Text)
                    t.TrangThai = 1
                    Dim kq As Integer = XL_TheTinDung.Them(t)
                    If kq <> 0 Then
                        HienThi()
                        MessageBox.Show("Them thanh cong!")
                    Else
                        MessageBox.Show("Them khong thanh cong!")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End Try
    End Sub

    Private Sub btnsuatk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuatk.Click
        If (lsv_dsthe.SelectedItems.Count > 0) Then
            Try
                Dim t As TheTinDung
                t = lsv_dsthe.SelectedItems(0).Tag
                t.NgayLap = dtp_ngaylap.Value.Date
                t.TaiKhoan = txttenthe.Text
                t.SoTien = Integer.Parse(txt_sotien.Text)
                t.HanMuc = Integer.Parse(txt_hanmuc.Text)
                t.LaiSuat = Integer.Parse(txt_laisuat.Text)
                Dim kq As Integer = XL_TheTinDung.CapNhat(t)
            If kq <> 0 Then
                HienThi()
                MessageBox.Show("Cập nhật thành công!")
            Else
                MessageBox.Show("Cập nhật không thành công!")
            End If
            Catch ex As Exception
                MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Exit Sub
            End Try
        Else
            MessageBox.Show("Chưa chọn vị trí cập nhật!", "Chú ý!")
        End If
        
    End Sub

    Private Sub lsv_dsthe_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsv_dsthe.SelectedIndexChanged
        Try
            Dim t As TheTinDung = lsv_dsthe.SelectedItems(0).Tag
            dtp_ngaylap.Value = t.NgayLap
            txttenthe.Text = t.TaiKhoan
            txt_sotien.Text = t.SoTien.ToString()
            txt_hanmuc.Text = t.HanMuc.ToString()
            txt_laisuat.Text = t.LaiSuat.ToString()
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub btnxoatk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoatk.Click
        If (lsv_dsthe.SelectedItems.Count > 0) Then
            If MessageBox.Show("Ban co muon xoa vi tri da chon ?", "Chu y!", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Try
                    Dim t As TheTinDung = lsv_dsthe.SelectedItems(0).Tag
                    Dim kq As Integer = XL_TheTinDung.Xoa(t)
                    HienThi()
                    If kq <> 0 Then
                        HienThi()
                        MessageBox.Show("Xoa thanh cong!")
                    Else
                        MessageBox.Show("Xoa khong thanh cong")
                    End If
                Catch ex As Exception
                    Exit Sub
                End Try
            End If
        Else
            MessageBox.Show("Ban chua chon vi tri xoa!")
        End If
    End Sub
End Class